import 'jquery';
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueResource from 'vue-resource';
import App from './App';
import Vant from 'Vant';
import 'vant/lib/vant-css/index.css';
import 'common/stylus/index.styl';
import 'src/utils/concrete';
import './components/personalCenter/utils';
import store from '../src/store/store'

import nearPark from '../src/components/nearPark/nearPark';
import reservePark from '../src/components/reservePark/reservePark';
import reserverPay from '../src/components/reserverPay/reserverPay'
import fastPay from './components/fastPay/fastPay'
import ZQIntro from '../src/components/ZQIntro/ZQIntro'
import baimaiQuan from '../src/components/baimaiQuan/baimaiQuan'
import baiQunCun from '../src/components/baiquanCun/baiQunCun'
import PayDetail from '../src/components/PayDetail/PayDetail'
import ReserveBindNo from  '../src/components/ReserveBindNo/ReserveBindNo'
import ReserveBindCar from '../src/components/ReserveBindCar/ReserveBindCar'
//个人中心
import complaintProposal from './components/personalCenter/complaintProposal'
import paymentRecord from './components/personalCenter/paymentRecord'
import reservationRecord from './components/personalCenter/reservationRecord'
import supplementList from './components/personalCenter/supplementList'
import VehicleManagement from './components/personalCenter/VehicleManagement'
import unlawfulReporting from './components/personalCenter/unlawfulReporting'
import boundVehicle from './components/personalCenter/boundVehicle'
import searchVehliceList from './components/personalCenter/searchVehliceList'
import ticketList from './components/personalCenter/ticketList'
import ticketEdit from './components/personalCenter/ticketEdit'
import upload from './components/personalCenter/upload'
import instructions from './components/personalCenter/instructions'
import bindPhone from './components/personalCenter/bindPhone'
import chpmt from './components/personalCenter/chpmt'







Vue.component('upload', upload)
import './common/font-awesome/css/font-awesome.min.css'

Vue.use(Vant);
Vue.use(VueRouter);
Vue.use(VueResource);
Vue.http.options.emulateJSON = true;

const routes = [
  {
    path: '/nearPark',
    name: 'nearPark',
    component: nearPark,
    meta: {
      title: '附近车场'
    }
  },
  {
    path: '/reservePark',
    name: 'reservePark',
    component: reservePark,
    meta: {
      title: '泊位预定'
    }
  },
  {
    path: '/reserverPay',
    name: 'reserverPay',
    component: reserverPay,
    meta: {
      title: '支付费用'
    }
  },
  {
    path: '/fastPay',
    name: 'fastPay',
    component: fastPay,
    meta: {
      title: '停车缴费'
    }
  },
  // 个人中心
  {
    path: '/complaintProposal',
    name: 'complaintProposal',
    component: complaintProposal,
    meta: {
      title: '投诉建议'
    }
  },
  {
    path: '/paymentRecord',
    name: 'paymentRecord',
    component: paymentRecord,
    //  meta: {
    //   title: '我的缴费'
    // }
  },
  {
    path: '/reservationRecord',
    name: 'reservationRecord',
    component: reservationRecord,
    meta: {
      title: '我的预约'
    }
  },
  {
    path: '/supplementList',
    name: 'supplementList',
    component: supplementList,
    meta: {
      title: '我的补缴'
    }
  },
  {
    path: '/VehicleManagement',
    name: 'VehicleManagement',
    component: VehicleManagement,
    meta: {
      title: '我的车辆'
    }
  },

  {
    path: '/boundVehicle',
    name: 'boundVehicle',
    component: boundVehicle,
    //  meta: {
    //   title: '新增绑定车辆'
    // }
  },

  {
    path: '/instructions',
    name: 'instructions',
    component: instructions,
    //  meta: {
    //   title: '新增绑定车辆'
    // }
  },
  {
    path: '/unlawfulReporting',
    name: 'unlawfulReporting',
    component: unlawfulReporting,
    meta: {
      title: '违法上报'
    }
  },
  {
    path: '/searchVehliceList',
    name: 'searchVehliceList',
    component: searchVehliceList,
    meta: {
      title: '车在哪'
    }
  },

  {
    path: '/ticketList',
    name: 'ticketList',
    component: ticketList,
    meta: {
      title: '我要发票'
    }
  }, {
    path: '/ticketEdit',
    name: 'ticketEdit',
    component: ticketEdit,
    //  meta: {
    //   title: '开票信息'
    // }
  }, {
    path: '/bindPhone',
    name: 'bindPhone',
    component: bindPhone,
    meta: {
      title: '验证手机号'
    }
  },
{
    path: '/chpmt',
    name: 'chpmt',
    component: chpmt,
    meta: {
      title: '车场平面图'
    }
  },
  {
    path: '/ZQIntro',
    name: 'ZQIntro',
    component: ZQIntro,
    meta: {
      title: '章丘简介'
    }
  },
  {
    path: '/baimaiQuan',
    name: 'baimaiQuan',
    component: baimaiQuan,
    meta: {
      title: '百脉泉'
    }
  },
  {
    path: '/baiQunCun',
    name: 'baiQunCun',
    component: baiQunCun,
    meta: {
      title: '白泉古村'
    }
  },
  {
    path: '/PayDetail',
    name: 'PayDetail',
    component: PayDetail,
    meta: {
      title: '费用详情'
    }
  },
  {
    path: '/ReserveBindNo',
    name: 'ReserveBindNo',
    component: ReserveBindNo,
    meta: {
      title: '预约-绑定手机号'
    }
  },
  {
    path: '/ReserveBindCar',
    name: 'ReserveBindCar',
    component: ReserveBindCar,
    meta: {
      title: '预约-绑定车辆'
    }
  }
];

const router = new VueRouter({
  mode: 'history',
  linkActiveClass: 'active',
  routes
});

import { UserService } from './components/personalCenter/api'
 import {baseUrl,zqAppid} from 'config/index'
let needPhone = ['VehicleManagement', 'boundVehicle'];
let needCar = ['searchVehliceList',  'supplementList', 'ticketEdit'];
let jnOpenId = ['reservationRecord', 'ticketList'];

 let  _zqVehicleManagementUrl=`http://wx.jinanparking.com/getCode.html?appid=${zqAppid}&scope=snsapi_base&state=wxPay&redirect_uri=${baseUrl}/VehicleManagement&scope=snsapi_base&state=1#wechart_redirect`

 let  _zqBindPhoneUrl=`http://wx.jinanparking.com/getCode.html?appid=${zqAppid}&scope=snsapi_base&state=wxPay&redirect_uri=${baseUrl}/bindPhone&scope=snsapi_base&state=1#wechart_redirect`

const getQueryString = (path, name) => {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = path.match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

router.beforeEach((to, from, next) => {
  let ph = $.inArray(to.name, needPhone) > -1;
  let nc = $.inArray(to.name, needCar) > -1;
  let jn = $.inArray(to.name, jnOpenId) > -1;
  let user=to.query.userInfo?JSON.parse(to.query.userInfo):'';

  if (ph || nc||jn) {
if(jn&&user){

if(!user.phoneNo){
window.location.href=_zqBindPhoneUrl
return;
}
if(!user.haveBindCar){
       window.location.href=_zqVehicleManagementUrl;
return;
}
  if (to.meta.title) {
      document.title = to.meta.title
  }
 next();


}else{
   UserService.findWxUserInfo(to.query.code).then((res) => {
    // let res={
    //   haveBindCar:true,
    //   openid:'123456',
    //   phoneNo:'123456'
    // }
      let _preUrl = getQueryString(to.fullPath, 'preUrl');
      let toBindPhone = { name: 'bindPhone', query: $.extend(to.query, { openId: res.openid, preUrl: (to.name == 'bindPhone' ? _preUrl : to.name) }) };
      let toVehMgt = { name: 'VehicleManagement', query: $.extend(to.query, { openId: res.openid, preUrl: (to.name == 'VehicleManagement' ? _preUrl : to.name) }) };
      if(jn&&res){
         res['zqCode']=to.query.code;
       // 页面跳转
        var  _codeUrl=`http://wx.jinanparking.com/getCode.html?appid=wx7305253d6ea42110&scope=snsapi_base&state=wxPay&redirect_uri=${baseUrl}/${to.name}?userInfo=${JSON.stringify(res)}`
        window.location.href=_codeUrl
      }
      if (to.meta.title) {
              document.title = to.meta.title
            }
      if (nc) {
        if (res.haveBindCar) {
          if (res.phoneNo) {
              next()
          } else {
             document.title ='验证手机号';
            next(toBindPhone)
          }
        } else {
          if (!res.phoneNo) {
            document.title ='验证手机号';
            next(toBindPhone)
          } else {
            document.title ='我的车辆';
            next(toVehMgt)
          }
        }
      } else if (ph) {
        if (res.phoneNo) {
              next()

        } else {
          document.title ='验证手机号';
          next(toBindPhone)
        }
      }
    }, (err) => {
       return;
    });
    }
  } else {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
      document.title = to.meta.title
    }
    next()
  }
})
/* eslint-disable no-new */
window.bvue = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
});
